#!/usr/bin/env bash

echo "Update containers script."

REGISTRY=$1
if [[ $REGISTRY == '' ]]; then echo "No docker registry"; exit 1; fi

echo "Update the docker containers."
docker pull ${REGISTRY}/ossrs/node:slim &&
docker pull ${REGISTRY}/ossrs/prometheus &&
docker pull ${REGISTRY}/ossrs/redis_exporter &&
docker pull ${REGISTRY}/ossrs/node-exporter &&
docker pull ${REGISTRY}/ossrs/certbot &&
docker pull ${REGISTRY}/ossrs/redis &&
docker pull ${REGISTRY}/ossrs/srs:node-av
if [[ $? -ne 0 ]]; then echo "Update the containers failed"; exit 1; fi

# Remove the containers if need upgrade
#   update_containers registry image as_image
#   update_containers registry.cn-hangzhou.aliyuncs.com ossrs/srs:4 ossrs/srs:4
#   update_containers registry.cn-hangzhou.aliyuncs.com ossrs/srs-terraform:hooks-1 ossrs/srs-cloud:hooks-1
#   update_containers registry.cn-hangzhou.aliyuncs.com ossrs/srs-cloud:hooks-1 ossrs/srs-cloud:hooks-1
function update_containers() {
  REGISTRY=$1 && IMAGE=$2 && AS_IMAGE=$3
  echo "Update containers for REGISTRY=$REGISTRY IMAGE=$IMAGE"

  # We must store the container images ID before pull, which will change it.
  IMAGE_IDS=$(docker ps -a | grep $IMAGE | awk '{print $1}')
  echo "The containers for $REGISTRY/$IMAGE is $IMAGE_IDS"

  if [[ -z  $IMAGE_IDS ]]; then
    echo "No containers for $REGISTRY/$IMAGE"
    return 0
  fi

  docker pull $REGISTRY/$IMAGE &&
  docker pull $REGISTRY/$AS_IMAGE
  if [[ $? -ne 0 ]]; then echo "Update the image $REGISTRY/$IMAGE as $AS_IMAGE failed"; exit 1; fi

  for IMAGE_ID in $IMAGE_IDS; do
    LATEST=$(docker inspect --format "{{.Id}}" $REGISTRY/$AS_IMAGE)
    RUNNING=$(docker inspect --format "{{.Image}}" $IMAGE_ID)
    NAME=$(docker inspect --format '{{.Name}}' $IMAGE_ID |sed 's|/||g')
    echo "Check $NAME RUNNING=$RUNNING, LATEST=$LATEST"

    if [[ $RUNNING != $LATEST ]]; then
      echo "Upgrade container $NAME from $RUNNING to $LATEST"
      docker rm -f $NAME
    fi
  done
}

# Update the containers before restart. Note that the containers are normally started by previous mgmt, with the
# upgraded image. So we only upgrade the images for all containers, and it's the responsibility for mgmt to restart the
# container if need to change the startup arguments.
echo "Upgrading all containers by images" &&
update_containers $REGISTRY ossrs/srs:4 ossrs/srs:4 &&
update_containers $REGISTRY ossrs/srs:5 ossrs/srs:5 &&
# Compatible for previous versions.
update_containers $REGISTRY ossrs/srs-terraform:hooks-1 ossrs/srs-cloud:hooks-1 &&
update_containers $REGISTRY ossrs/srs-terraform:tencent-1 ossrs/srs-cloud:tencent-1 &&
update_containers $REGISTRY ossrs/srs-terraform:ffmpeg-1 ossrs/srs-cloud:ffmpeg-1 &&
# For new versions.
update_containers $REGISTRY ossrs/srs-cloud:hooks-1 ossrs/srs-cloud:hooks-1 &&
update_containers $REGISTRY ossrs/srs-cloud:tencent-1 ossrs/srs-cloud:tencent-1 &&
update_containers $REGISTRY ossrs/srs-cloud:ffmpeg-1 ossrs/srs-cloud:ffmpeg-1
if [[ $? -ne 0 ]]; then echo "Upgrade containers failed"; exit 1; fi

# Note that we should update the platform image, so we could fast startup the container, because mgmt use another tag
# which has the same hash.
echo "Update platform image" && docker pull $REGISTRY/ossrs/srs-cloud:platform-1
if [[ $? -ne 0 ]]; then echo "Update platform image failed"; exit 1; fi

# Removed unreferenced image, the TAG is <none>
for image in $(docker images |grep '<none>' |awk '{print $3}'); do
  docker rmi -f $image
  echo "Remove image $image, r0=$?"
done

# Try to eliminate other images, except current registry.
#   [IN USE] docker.io
#   [IN USE] registry.cn-hangzhou.aliyuncs.com
#   [NO USE] sgccr.ccs.tencentyun.com
#   [NO USE] ccr.ccs.tencentyun.com
SOURCES=("docker.io" "registry.cn-hangzhou.aliyuncs.com" "sgccr.ccs.tencentyun.com" "ccr.ccs.tencentyun.com")
FILTER="keepit"
for SOURCE in ${SOURCES[*]}; do
  FILTER="$FILTER|$SOURCE"
  if [[ $REGISTRY == $SOURCE ]]; then continue; fi
  for IMAGE in $(docker images | grep "^$SOURCE"| awk '{print $1":"$2}'); do
    echo "Try to eliminate $IMAGE in quiet"
    docker rmi $IMAGE >/dev/null 2>&1
  done
done
# Process for docker.io, without any prefix.
if [[ $REGISTRY != 'docker.io' ]]; then
  for IMAGE in $(docker images | grep -vE "$FILTER"| awk 'NR>1 {print $1":"$2}'); do
    echo "Try to eliminate $IMAGE in quiet"
    docker rmi $IMAGE >/dev/null 2>&1
  done
fi

